package com.blog.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PropertiesUtil {
	
	//日志
	private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
	
	private static Properties prop;
	
	//静态代码块.  优于创建对象之前和普通代码块之前执行.
	static{  //路径一定是包名+类名
		String fileName = "properties/blog.properties";
		prop = new Properties();
		try {
			prop.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName), "utf-8"));
		} catch (IOException e) {
			logger.error("配置文件读取异常",e);
		}
	}
	
	public static String getProperty(String key){
		String value = prop.getProperty(key.trim());
		if(StringUtils.isBlank(value)){
			return null;
		}
		return value.trim();
	}
	
	//重载方法,可以设置默认值
	public static String getProperty(String key, String defaultValue){
		
		String value = prop.getProperty(key.trim());
		if(StringUtils.isBlank(value)){
			value = defaultValue;
		}
		return value.trim();
	}
	
}
